package com.example.zhinongbao.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.example.zhinongbao.R;
import com.example.zhinongbao.entity.TitleInfo;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class XTActivityFragment extends Fragment {
   // private String[] titles = {"娱乐", "军事", "教育", "文化", "将康", "财经", "体育", "汽车", "科技"};
    private List<TitleInfo> titles=new ArrayList<>();
    private TabLayout tab_layout;
    private ViewPager2 viewPager;
//    private ImageView mImageView;
    private View school ;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       if (school== null){
           school = inflater.inflate(R.layout.app_bar_main , container , false);
       }

            //  初始化title数据
            titles.add(new TitleInfo("推荐","top"));
            titles.add(new TitleInfo("国内","guonei"));
            titles.add(new TitleInfo("国际","guoji"));
            titles.add(new TitleInfo("娱乐","yule"));
            titles.add(new TitleInfo("体育","tiyu"));
            titles.add(new TitleInfo("军事","junshi"));
            titles.add(new TitleInfo("科技","keji"));
            titles.add(new TitleInfo("财经","cajing"));
            titles.add(new TitleInfo("游戏","youxi"));
            titles.add(new TitleInfo("汽车","qiche"));
            titles.add(new TitleInfo("健康","jiankang"));
            //初始化控件
            tab_layout = school.findViewById(R.id.tab_layout);
            viewPager = school.findViewById(R.id.viewPager);
//            mImageView=school.findViewById(R.id.image1);
//            mImageView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    getActivity().finish();
//                }
//            });
            // viewPager需要设置一个adapter
            viewPager.setAdapter(new FragmentStateAdapter(this) {
                @NonNull
                @Override
                public Fragment createFragment(int position) {
                    //创建 NewsTabFragment页面
                    String title = titles.get(position).getPy_title();
                    TabNewsFragment tabNewsFragment = TabNewsFragment.newInstance(title);
                    return tabNewsFragment;
                }

                @Override
                public int getItemCount() {
                    return titles.size();
                }
            });
            tab_layout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    //设置viewPager选中当前页
                    viewPager.setCurrentItem(tab.getPosition(), false);
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {

                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });
            //将tab_layout和 viewPager关联在一起
            TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tab_layout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
                @Override
                public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                    tab.setText(titles.get(position).getTitle());
                }
            });
            //这几话不能少
            tabLayoutMediator.attach();

        return school;

    }}

